var geocoder = new google.maps.Geocoder();

function geocodePosition(pos) {
   geocoder.geocode({
		latLng: pos
	}, function(responses) {
		if (responses && responses.length > 0) {
			updateMarkerAddress(responses[0].formatted_address);
		} else {
			updateMarkerAddress('Cannot determine address at this location.');
		}
	});
}

function getLatLngByAddress(address) {
	var latLng;
	geocoder.geocode({
		'address': address
	}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			var latitude = results[0].geometry.location.lat();
			var longitude = results[0].geometry.location.lng();
			latLng = {
				"lat": latitude,
				"lng": longitude
			};
		}
	});
	console.log(latLng);
	return latLng;
}
var ll = getLatLngByAddress("Hà N?i");
//console.log(ll);

function updateMarkerStatus(str) {
	document.getElementById('markerStatus').innerHTML = str;
}

function updateMarkerPosition(latLng) {
	document.getElementById('info').innerHTML = [
	latLng.lat(), latLng.lng()].join(', ');
}

function updateMarkerAddress(str) {
	document.getElementById('address').innerHTML = str;
}

function maptoAddress(address) {
	geocoder.geocode({
		'address': address
	}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			var latitude = results[0].geometry.location.lat();
			var longitude = results[0].geometry.location.lng();
			initialize(latitude, longitude)
		}
	});
}



function initialize(lat, lng) {
   var latLng = new google.maps.LatLng(lat, lng);
	var map = new google.maps.Map(document.getElementById('mapCanvas'), {
		zoom: 17,
		center: latLng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	});
	var marker = new google.maps.Marker({
		position: latLng,
		title: 'Point A',
		map: map,
		draggable: true
	});
	// Update current position info.
	updateMarkerPosition(latLng);
	geocodePosition(latLng);
	// Add dragging event listeners.
	google.maps.event.addListener(marker, 'dragstart', function() {
		updateMarkerAddress('Đang tìm...');
	});
	google.maps.event.addListener(marker, 'drag', function() {
		updateMarkerStatus('Đang tìm...');
		updateMarkerPosition(marker.getPosition());
	});
	google.maps.event.addListener(marker, 'dragend', function() {
		updateMarkerStatus('Drag ended');
		geocodePosition(marker.getPosition());
	});
}